#include <QDockWidget>
#include <QListView>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QStandardItemModel>
#include <QMessageBox>

#include "mainwindow.h"
#include "paintview.h"
#include "painttool.h"
#include "rectangletool.h"
#include "tools.h"
#include "toolsmanager.h"
#include "paintscene.h"
#include "itemsmanager.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    currentTool = NULL;
    createDockWindows();
    createMainWidget();
}

MainWindow::~MainWindow()
{

}

void MainWindow::createDockWindows()
{
    QDockWidget *toolDock = new QDockWidget(tr("Tools"), this);
    toolDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);

    toolsList = new QListView(this);
    toolsList->setModel(toolsListModel = new ToolsListModel(this));
    //TODO: not the right place
    toolsListModel->addTool(new RectangleTool());
    toolsListModel->addTool(new CircleTool());

    toolDock->setWidget(toolsList);
    addDockWidget(Qt::RightDockWidgetArea, toolDock);

    connect(toolsList, SIGNAL(clicked(QModelIndex)), this, SLOT(recvClick(QModelIndex)));
    connect(this, SIGNAL(toolSelected(PaintTool*)), ToolsManager::instance(), SLOT(toolSelect(PaintTool*)));

}

void MainWindow::createMainWidget()
{
    this->scene = new PaintScene(0,0,600,600);
    this->view = new PaintView(this->scene, (QWidget*) this);
    this->setCentralWidget(this->view);
    ItemsManager::instance()->setPaintScene((PaintScene*) scene);
}

PaintTool* MainWindow::getSelectedTool()
{
    return currentTool;
}

void MainWindow::recvClick(const QModelIndex &index)
{
    currentTool = toolsListModel->selectedTool(index.row());
    emit toolSelected(currentTool);
}
